/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.repository;

import me.zhengjie.modules.system.domain.PurchaseData;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.sql.Timestamp;
import java.util.List;
import java.util.Set;

/**
* @website https://eladmin.vip
* @author 李忠臻
* @date 2024-02-20
**/
public interface PurchaseDataRepository extends JpaRepository<PurchaseData, Long>, JpaSpecificationExecutor<PurchaseData> {

    List<PurchaseData> findByPurchaseRequestIdIn(List purchaseRequestIds);

    List<PurchaseData> findByB2bIn(Set b2b);
    /**
    * 根据 Index 查询
    * @param index /
    * @return /
    */
    PurchaseData findByIndex(Integer index);

    List<PurchaseData> findByB2bAndModelOrderByIndex(String b2b, String model);

    List<PurchaseData> findByCustomerAndModel(String customer, String model);

    @Query( value = "select d.* from purchase_data d where DATEDIFF(NOW(), d.create_time) < ?2 and DATEDIFF(NOW(), d.create_time) > ?3 and d.model = ?1 and if(?4 != '', customer = ?4, 1 = 1) ",
            nativeQuery = true )
    List<PurchaseData> findLastDataByModel(@Param("model") String model, @Param("fromNowDay") Integer fromNowDay,
                                           @Param("greaterFromNowDay") Integer greaterFromNowDay, @Param("customer") String customer);

//    @Query( "select d.model, ?1 as from_now_day, count(*) as purchase_time " +
//            "from purchase_data d where DATEDIFF(NOW(), d.create_time) < ?1 group by d.model having count(*) <= ?2 " )
//    Page<SalePoorProduct> querySalePoorProduct(@Param("fromNowDay") Integer fromNowDay,
//                                               @Param("purchaseTime") Integer purchaseTime, Pageable pageable);
//
//    @Query( "select d.model, ?1 as from_now_day, count(*) as purchase_time " +
//            "from purchase_data d where DATEDIFF(NOW(), d.create_time) < ?1 group by d.model having count(*) >= ?2 " )
//    List<SaleWellProduct> querySaleWellProduct(@Param("fromNowDay") Integer fromNowDay,
//                                               @Param("purchaseTime") Integer purchaseTime);
//
//    @Query( "select d.model, ?1 as from_now_day, count(*) as purchase_time " +
//            "from purchase_data d where DATEDIFF(NOW(), d.create_time) < ?1 group by d.model having count(*) <= ?2  " )
//    List<SalePoorProduct> querySalePoorProduct(@Param("fromNowDay") Integer fromNowDay,
//                                               @Param("purchaseTime") Integer purchaseTime);
//    @Query(value = "select min(id) as id, supplier_name, sum(tax_amount) as tax_amount" +
////            " min(create_by) as create_by, min(create_time) as create_time, min(update_by) as update_by, min(update_time) as update_time" +
//            " from purchase_data d where godown_entry_date between ?1 and ?2 group by supplier_name",
//            nativeQuery = true)
//    List<PurchaseSettlementData> getSupplierSettlmentData(@Param("godownEntryDateStart") Timestamp godownEntryDateStart,
//                                                          @Param("godownEntryDateEnd") Timestamp godownEntryDateEnd);
}